package cm.iccsoft.android.telecomcost.adapter;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.activities.ProviderListActivity;
import cm.iccsoft.android.telecomcost.entities.Provider;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ProviderAdapter extends ArrayAdapter<Provider> {
	private ArrayList<Provider> items;
	private Context _context;
	private Activity _activity;

	public ProviderAdapter(Context context, int textViewResourceId,
			ArrayList<Provider> items, Activity activity) {
		super(context, textViewResourceId, items);
		this.items = items;
		_context = context;
		_activity = activity;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public ProviderAdapter(Context context, int resource,
			int textViewResourceId, ArrayList<Provider> objects) {
		super(context, resource, textViewResourceId, objects);
		this.items = objects;
		_context = context;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 */
	public ProviderAdapter(Context context, int resource, int textViewResourceId) {
		super(context, resource, textViewResourceId);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public ProviderAdapter(Context context, int textViewResourceId,
			List<Provider> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public ProviderAdapter(Context context, int textViewResourceId,
			Provider[] objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 */
	public ProviderAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i("ProviderAdapter", "getting provider view");
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(
					cm.iccsoft.android.telecomcost.R.layout.provider_view, null);
		}

		final Provider provider = items.get(position);
		if (provider != null) {
			final CheckBox chk = (CheckBox) v
					.findViewById(cm.iccsoft.android.telecomcost.R.id.checkBox1);
			if (chk != null) {
				chk.setText(provider.toString());
				chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					public void onCheckedChanged(CompoundButton arg0,
							boolean arg1) {
						selectProvider(provider, arg1);
						chk.setChecked(provider.isSelected());
					}
				});
			}

			final Button btnEditProvider = (Button) v
					.findViewById(cm.iccsoft.android.telecomcost.R.id.btnEditProvider);
			if (btnEditProvider != null) {
				btnEditProvider.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						showEditDialog(provider);
					}
				});

			}
		}
		return v;
	}

	protected void showEditDialog(Provider provider) {
		if (_activity.toString() == "ProviderListActivity") {
			Bundle data = new Bundle();	
			Gson gson = TelecomCost.getGson();
			data.putString(Provider.PROP_TYPE,gson.toJson(provider));			
			_activity.showDialog(ProviderListActivity.DIALOG_EDIT_PROVIDER,
					data);
		}
	}

	protected void selectProvider(Provider provider, boolean arg1) {
		provider.setSelected(arg1);

		if (arg1) {
			Log.i("Provider_ListView", "zone " + provider.getName()
					+ " checked");
		} else {
			Log.i("Provider_ListView", "zone " + provider.getName()
					+ " unchecked");
		}
	}
}
